$(function () {
    new AjaxUpload('#upload', {
        action: baseURL + "sys/upload/imgUpload",
        name: 'imgFile',
        autoSubmit: true,
        responseType: "json",
        onComplete: function (file, r) {
            if (r.code == 0) {
                alert("图片上传成功！！");

                vm.btCustomerFirstpage.imgLink +=";"+ r.url;
                if( vm.btCustomerFirstpage.imgLink[0]==";"){
                    vm.btCustomerFirstpage.imgLink=  vm.btCustomerFirstpage.imgLink.substr(1);
                }
                vm.imgarr= vm.btCustomerFirstpage.imgLink.split(";");
            } else {
                alert(r.msg);
            }
        }
    });
});
var editor;
//文本编辑器
function kedit(kedit) {
    // var clearDefault=true;//是否有默认文字信息
    editor = KindEditor.create(kedit, {
        width: '680px',
        height: '400px',
        resizeMode: 0,
        allowPreviewEmoticons: false,
        allowImageUpload: true,//允许上传图片
        allowFileManager: true, //允许对上传图片进行管理
        uploadJson: baseURL+'sys/upload/imgUpload', //上传图片的java代码
        fileManagerJson: baseURL+'/upload/fileManager',
        afterUpload: function () {
            this.sync();
        }, //图片上传后，将上传内容同步到textarea中
        afterBlur: function () {
            this.sync();
        }, //失去焦点时，将上传内容同步到textarea中

        items: [
            'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'cut', 'copy', 'paste',
            'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
            'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
            'superscript', 'clearhtml', 'selectall', '|', 'fullscreen', '/',
            'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
            'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image',
            'table', 'hr', 'emoticons', 'baidumap','media'
        ]
    });
};
var vm = new Vue({
	el:'#rrapp',
	data:{
		btCustomerFirstpage: {id:null},
        imgarr:[]
	},
	methods: {
        add:function () {

        },
		saveOrUpdate: function (event) {
			var url = vm.btCustomerFirstpage.id == null ? "sys/btcustomerfirstpage/save" : "sys/btcustomerfirstpage/update";
            vm.btCustomerFirstpage.yhGongao = encodeURIComponent($("#activity_detail").val());
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.btCustomerFirstpage),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							//vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        deleteimg:function (img) {
            vm.imgarr.splice(vm.imgarr.indexOf(img),1);
            vm.btCustomerFirstpage.imgLink=vm.imgarr.join(';');
        },
		getInfo: function(id){
			$.get(baseURL + "sys/btcustomerfirstpage/info", function(r){
				if(r.btCustomerFirstpage==null){
                    r.btCustomerFirstpage.id = 0;
                    vm.showdetail = true;
                    vm.btCustomerFirstpage = {imgLink:""};
                    vm.imgarr=[];
				}else{
                    vm.btCustomerFirstpage = r.btCustomerFirstpage;
                    if(vm.btCustomerFirstpage.imgLink==null|| vm.btCustomerFirstpage.imgLink==""){
                        vm.imgarr=null;
                        vm.btCustomerFirstpage.imgLink=""
                    }else{
                        vm.imgarr= vm.btCustomerFirstpage.imgLink.split(";");
                    }
				}
                var t;
                clearTimeout(t);
                KindEditor.remove('textarea[name="contentDetail"]');
                t = setTimeout(function (){
                    kedit('textarea[name="contentDetail"]');
                }, 500);
            });
		}
	},
    mounted: function () {
        this.getInfo();
    }
});